package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.*;
import cc.rengu.igas.bomp.common.dao.impl.*;
import cc.rengu.igas.bomp.common.entity.*;
import cc.rengu.igas.bomp.common.enums.*;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.NeedInspectMchntInfoBean;
import cc.rengu.igas.bomp.facade.request.InspectTaskMchntListQueryRequest;
import cc.rengu.igas.bomp.facade.response.InspectTaskMchntListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/04/17
 */
public class InspectTaskMchntListQueryService extends ManageService {
    /**
     * 随机数
     */
    private Random random = new Random();

    @Override
    protected Object bizBeforeCust() throws Exception {
        InspectTaskMchntListQueryRequest inspectTaskMchntListQueryRequest = new InspectTaskMchntListQueryRequest();
        ConvertUtil.convertOutput(inspectTaskMchntListQueryRequest);
        return inspectTaskMchntListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        InspectTaskMchntListQueryRequest inspectTaskMchntListQueryRequest = (InspectTaskMchntListQueryRequest) request;
        InspectTaskMchntListQueryResponse inspectTaskMchntListQueryResponse = new InspectTaskMchntListQueryResponse();
        /*获取请求参数：法人行，巡检任务类型，行业类别，商户属性，签约机构，商户类型，历史巡检方式，商户等级，抽检数量，抽检方式，月份，交易笔数，风险等级，开始日期，截止日期，当前页面，页面当前记录数*/
        String instId = inspectTaskMchntListQueryRequest.getHeader().getInstId();
        /*用户标识*/
        String userId = inspectTaskMchntListQueryRequest.getUserId();
        /*巡检任务类型*/
        String inspectTaskType = inspectTaskMchntListQueryRequest.getInspectTaskType();
        /*行业类别*/
        String industryType = inspectTaskMchntListQueryRequest.getIndustryType();
        /*商户属性*/
        String mchntAttr = inspectTaskMchntListQueryRequest.getMchntAttr();
        /*签约机构*/
        String signInstId = inspectTaskMchntListQueryRequest.getSignInstId();
        /*商户类型*/
        String mchntType = inspectTaskMchntListQueryRequest.getMchntType();
        /*历史巡检方式*/
        String inspectType = inspectTaskMchntListQueryRequest.getInspectType();
        /*商户等级*/
        String mchntLevel = inspectTaskMchntListQueryRequest.getMchntLevel();
        /*商户号*/
        String mchntNo = inspectTaskMchntListQueryRequest.getMchntNo();
        /*抽检数量*/
        String spotCheckMchntNum = inspectTaskMchntListQueryRequest.getSpotCheckMchntNum();
        /*抽检方式*/
        String spotCheckMethod = inspectTaskMchntListQueryRequest.getSpotCheckMethod();
        /*月份*/
        String lowVolumeMonth = inspectTaskMchntListQueryRequest.getLowVolumeMonth();
        /*交易笔数小于数量*/
        String lowVolumeLessThan = inspectTaskMchntListQueryRequest.getLowVolumeLessThan();
        /*风险等级*/
        String riskLevel = inspectTaskMchntListQueryRequest.getRiskLevel();
        /*开始日期*/
        String startDate = inspectTaskMchntListQueryRequest.getStartDate();
        /*截止日期*/
        String endDate = inspectTaskMchntListQueryRequest.getEndDate();
        /*必填性校验*/
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(inspectTaskType)) {
            rglog.error("新建巡检任务商户列表查询交易必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /*根据用户标识查询用户所属机构*/
        AuthUserMapper authUserMapper = new AuthUserMapperImpl();
        AuthUser authUser = authUserMapper.selectAuthUserById(userId);
        if (null == authUser) {
            rglog.error("查询用户信息失败");
            throw new BizException(RespCodeEnum.QUERY_AUTH_USER_IS_NULL.getRespCode(), RespCodeEnum.QUERY_AUTH_USER_IS_NULL.getRespDesc());
        }
        String orgCode = authUser.getOrgCode();

        /*当前页数*/
        int pageIndex = inspectTaskMchntListQueryRequest.getPageIndex();
        /*页显示记录数*/
        int pageSize = inspectTaskMchntListQueryRequest.getPageSize();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);

        /*查询条件赋值*/
        /*法人行查询*/
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        /*行业类别查询条件赋值*/
        if (!StringUtil.isEmptyOrNull(industryType)) {
            mchntBaseInfo.setIndustryType(industryType);
        }
        /*商户类别（属性）查询条件赋值*/
        if (!StringUtil.isEmptyOrNull(mchntAttr)) {
            mchntBaseInfo.setMchntAttr(mchntAttr);
        }
        /*签约机构查询条件赋值*/
        if (!StringUtil.isEmptyOrNull(signInstId)) {
            mchntBaseInfo.setSignInstId(signInstId);
        }
        /*商户类型查询条件赋值*/
        if (!StringUtil.isEmptyOrNull(mchntType)) {
            mchntBaseInfo.setMchntType(mchntType);
        }
        /*商户等级查询条件赋值*/
        if (!StringUtil.isEmptyOrNull(mchntLevel)) {
            mchntBaseInfo.setMchntLevel(mchntLevel);
        }
        /*商户号查询条件赋值*/
        if (!StringUtil.isEmptyOrNull(mchntNo)) {
            mchntBaseInfo.setMchntNo(mchntNo);
        }
        /*商户入驻开始时间查询条件赋值*/
        if (!StringUtil.isEmptyOrNull(startDate)) {
            mchntBaseInfo.setMchntExpandTime(startDate);
        }
        /*商户入驻结束时间查询条件赋值*/
        if (!StringUtil.isEmptyOrNull(endDate)) {
            mchntBaseInfo.setRemark5(endDate);
        }
        /*查询商户历史巡检信息详情*/
        InspectTaskDetailMapper inspectTaskDetailMapper = new InspectTaskDetailMapperImpl();
        /*历史巡检方式查询条件*/
        if (!StringUtil.isEmptyOrNull(inspectType)) {
            mchntBaseInfo.setRemark3(inspectType);
        }
        /*商户状态赋值*/
        mchntBaseInfo.setMchntStatus(MchntStatusEnum.NORMAL.getStatus());
        /*非总行人员，当前登录用户所属机构赋值*/
        if (!orgCode.equals(instId + BompAppParamConstant.INSTORGCODE)) {
            mchntBaseInfo.setRemark4(orgCode);
        }

        /*商户信息列表*/
        List<NeedInspectMchntInfoBean> needInspectMchntInfoList = new ArrayList<>();
        /*商户基本信息列表*/
        List<MchntBaseInfo> mchntBaseInfoList = new ArrayList<MchntBaseInfo>();
        PageResult pageResult = null;
        /*根据巡检方式不同进行不同操作*/
        if (inspectTaskType.equals(InspectTaskTypeEnum.NORMAL_TYPE.getTaskType()) || inspectTaskType.equals(InspectTaskTypeEnum.NEW_SETTLE_TYPE.getTaskType())) {
            /*正常商户巡检或新入驻商户*/
            pageResult = queryNormalMchntInfoList(instId, mchntBaseInfo, pageInfo);
        } else if (inspectTaskType.equals(InspectTaskTypeEnum.SPOT_TYPE.getTaskType())) {
            /*抽检商户巡检*/
            pageResult = querySpotMchntInfoList(spotCheckMchntNum, spotCheckMethod, userId, instId, mchntBaseInfo, pageInfo);
        } else if (inspectTaskType.equals(InspectTaskTypeEnum.LOW_VOLUME_TYPE.getTaskType())) {
            /*交易量偏低商户巡检商户巡检*/
            pageResult = queryLowVolumeMchntInfoList(lowVolumeMonth, lowVolumeLessThan, instId, mchntBaseInfo, pageInfo);
        } else if (inspectTaskType.equals(InspectTaskTypeEnum.RISK_TYPE.getTaskType())) {
            /*风险商户巡检*/
            mchntBaseInfo.setRemark1("riskMchnt");
            pageResult = queryNormalMchntInfoList(instId, mchntBaseInfo, pageInfo);
            //    pageResult = queryRiskMchntInfoList(instId,mchntBaseInfo,pageInfo);
        } else {
            rglog.error("巡检任务类型参数非法！");
            throw new BizException(RespCodeEnum.ILLEGAL_OPERATION.getRespCode(), RespCodeEnum.ILLEGAL_OPERATION.getRespDesc());
        }
        if (pageResult != null) {
            pageInfo = pageResult.getPageInfo();
            mchntBaseInfoList = pageResult.getResult().stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
        }

        if (!CollectionUtils.isEmpty(mchntBaseInfoList)) {
            if (BompAppParamConstant.ALL_MCHNT_INSPECT_PAGE_SIZE == pageSize) {
                /*全部巡检，返回生成巡检任务明细所需的商户信息*/
                for (MchntBaseInfo mchntBaseInf : mchntBaseInfoList) {
                    NeedInspectMchntInfoBean needInspectMchntInfoBean = new NeedInspectMchntInfoBean();
                    needInspectMchntInfoBean.setMchntNo(mchntBaseInf.getMchntNo());
                    //商户名称
                    needInspectMchntInfoBean.setMchntName(mchntBaseInf.getMchntName());
                    //商户类型
                    needInspectMchntInfoBean.setMchntType(mchntBaseInf.getMchntType());
                    //营业地址
                    needInspectMchntInfoBean.setMchntBizAddr(mchntBaseInf.getMchntBizAddr());
                    //行业类别
                    needInspectMchntInfoBean.setIndustryType(mchntBaseInf.getIndustryType());
                    //商户属性
                    needInspectMchntInfoBean.setMchntAttr(mchntBaseInf.getMchntAttr());
                    //商户等级
                    needInspectMchntInfoBean.setMchntLevel(mchntBaseInf.getMchntLevel());
                    //签约机构号
                    needInspectMchntInfoBean.setSignInstId(mchntBaseInf.getSignInstId());
                    //拓展经理工号
                    needInspectMchntInfoBean.setManagerId(mchntBaseInf.getManagerId());
                    //拓展经理名称
                    needInspectMchntInfoBean.setManagerName(mchntBaseInf.getManagerName());
                    needInspectMchntInfoList.add(needInspectMchntInfoBean);
                }
                /*拼装响应报文*/
                inspectTaskMchntListQueryResponse.setTotalPage(pageInfo.getTotalPage());
                inspectTaskMchntListQueryResponse.setPageIndex(pageInfo.getPageIndex());
                inspectTaskMchntListQueryResponse.setPageSize(pageInfo.getPageSize());
                inspectTaskMchntListQueryResponse.setTotalRows(pageInfo.getTotalRows());
                inspectTaskMchntListQueryResponse.setNeedInspectMchntInfoList(needInspectMchntInfoList);
                /* 按照接口转换基础产品信息并响应给请求方 */
                BizResponse<InspectTaskMchntListQueryResponse> bizResponse = new BizResponse<>();
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setResult(inspectTaskMchntListQueryResponse);
                return bizResponse;
            }
            /* 按照创建时间降序排序 */
            List<MchntBaseInfo> mchntBaseInfos = new ArrayList<>();
            for (MchntBaseInfo mchntBaseInf : mchntBaseInfoList) {
                List<InspectTaskDetail> inspectTaskDetails = inspectTaskDetailMapper.selectBompInspectTaskDetailByMchntNoOrInspectType(instId, mchntBaseInf.getMchntNo(), "");
                if (!CollectionUtils.isEmpty(inspectTaskDetails)) {
                    /*获取巡检时间最近的商户巡检任务详情*/
                    InspectTaskDetail inspectTaskDetail = inspectTaskDetails.get(0);
                    /*最近巡检方式赋值*/
                    mchntBaseInf.setRemark1(inspectTaskDetail.getInspectType());
                    /*最近巡检时间赋值*/
                    mchntBaseInf.setRemark2(inspectTaskDetail.getInspectTime());

                }
                mchntBaseInfos.add(mchntBaseInf);
            }
           /* mchntBaseInfos = mchntBaseInfos.stream().sorted(Comparator.comparing(MchntBaseInfo::getMchntExpandTime)).collect(Collectors.toList());
            Collections.reverse(mchntBaseInfos);*/
            inspectTaskMchntListQueryResponse.setTotalPage(pageInfo.getTotalPage());
            inspectTaskMchntListQueryResponse.setPageIndex(pageInfo.getPageIndex());
            inspectTaskMchntListQueryResponse.setPageSize(pageInfo.getPageSize());
            inspectTaskMchntListQueryResponse.setTotalRows(pageInfo.getTotalRows());
            mchntBaseInfos.forEach(item -> convMchntBaseInfo(item, needInspectMchntInfoList, lowVolumeMonth, instId, inspectTaskType));
        }
        /*拼装响应报文*/
        inspectTaskMchntListQueryResponse.setNeedInspectMchntInfoList(needInspectMchntInfoList);
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse<InspectTaskMchntListQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(inspectTaskMchntListQueryResponse);
        return bizResponse;
    }

    /**
     * 巡检商户信息转换
     * mchntBaseInfo 商户基本信息
     * needInspectMchntInfoList 可巡检商户信息列表
     */
    private void convMchntBaseInfo(MchntBaseInfo mchntBaseInfo, List<NeedInspectMchntInfoBean> needInspectMchntInfoList, String lowVolumeMonth, String instId, String inspectTaskType) {
        try {
            NeedInspectMchntInfoBean needInspectMchntInfo = new NeedInspectMchntInfoBean();
            BeanUtil.beanCopy(mchntBaseInfo, needInspectMchntInfo);
            /*商户入驻时间赋值*/
            needInspectMchntInfo.setEntryTime(mchntBaseInfo.getMchntExpandTime());
            /*最近巡检方式赋值*/
            needInspectMchntInfo.setPrevInspectType(mchntBaseInfo.getRemark1());
            /*最近巡检时间赋值*/
            needInspectMchntInfo.setCreateTime(mchntBaseInfo.getRemark2());
            /*查询签约机构名称*/
            OrganizationMapper organizationMapper = new OrganizationMapperImpl();
            Organization organization = organizationMapper.selectOrganizationByPrimaryKey(instId, mchntBaseInfo.getSignInstId());
            if (null != organization) {
                needInspectMchntInfo.setSignInstName(organization.getOrgName());
            }
            /*查询商户等级名称信息*/
            MchntLevelInfoMapper mchntLevelInfoMapper = new MchntLevelInfoMapperImpl();
            MchntLevelInfo mchntLevelInfo = mchntLevelInfoMapper.selectMchntLevelInfoByMchntLevelCode(instId, mchntBaseInfo.getMchntLevel());
            if (null != mchntLevelInfo) {
                needInspectMchntInfo.setMchntLevelName(mchntLevelInfo.getMchntLevelName());
            }
            /*交易量低商户查询二维码信息及*/
            if (inspectTaskType.equals(InspectTaskTypeEnum.LOW_VOLUME_TYPE.getTaskType())) {
                BompNoTxnRegMapper bompNoTxnRegMapper = new BompNoTxnRegMapperImpl();
                List<NoTxnReg> noTxnRegs = bompNoTxnRegMapper.selectBompNoTxnRegByMchntNo(instId, mchntBaseInfo.getMchntNo(), lowVolumeMonth);
                if (null != noTxnRegs && !noTxnRegs.isEmpty()) {
                    /*拼接二维码，终端编码信息*/
                    StringBuilder termNo = new StringBuilder();
                    for (NoTxnReg noTxnReg : noTxnRegs) {
                        if (StringUtil.isEmptyOrNull(noTxnReg.getTermNo())) {
                            termNo.append(noTxnReg.getTermNo()).append(",");
                        }
                    }
                    if (!StringUtil.isEmptyOrNull(termNo.toString())) {
                        needInspectMchntInfo.setLowVolumTermOrQrcode(termNo.toString().substring(0, termNo.length() - 1));
                    }
                }
            }
            if (inspectTaskType.equals(InspectTaskTypeEnum.RISK_TYPE.getTaskType())) {
                /*根据商户号查询商户具体风险，一个商户多条数据时如何描述？*/
            }
            needInspectMchntInfoList.add(needInspectMchntInfo);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("新建巡检任务商户查询列表信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
    }

    /**
     * 正常巡检商户查询
     */
    private PageResult queryNormalMchntInfoList(String instId, MchntBaseInfo mchntBaseInfo, PageInfo pageInfo) throws Exception {
        /*根据查询条件进行*/
        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        return mchntBaseInfoMapper.selectBompMchntBaseInfoByInspectTask(instId, mchntBaseInfo, pageInfo);
    }

    /**
     * 抽检商户巡检查询
     */
    private PageResult querySpotMchntInfoList(String spotCheckMchntNum, String spotCheckMethod, String userId, String instId, MchntBaseInfo mchntBaseInfo, PageInfo pageInfo) throws Exception {
        List<Object> mchntBaseInfoSpotList = new ArrayList<>();
        if (pageInfo.getPageSize() == BompAppParamConstant.ALL_MCHNT_INSPECT_PAGE_SIZE
                && !StringUtil.isEmptyOrNull(spotCheckMchntNum) && Integer.parseInt(spotCheckMchntNum) > BompAppParamConstant.ALL_MCHNT_INSPECT_PAGE_SIZE) {
            rglog.error("抽检商户全部巡检不能多于" + BompAppParamConstant.ALL_MCHNT_INSPECT_PAGE_SIZE);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        mchntBaseInfo.setRemark2("10000");
        /**查询商户基本信息*/
        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        /*当收单机构为空且按机构随机抽取时根据登录人查询所在机构及其下属机构*/
        if (!StringUtil.isEmptyOrNull(spotCheckMethod) && SpotTypeEnum.ORG_RANDOM.getSpotType().equals(spotCheckMethod)) {
            AuthUserMapper authUserMapper = new AuthUserMapperImpl();
            AuthUser authUser = authUserMapper.selectAuthUserById(userId);
            if (null == authUser) {
                rglog.error("查询用户信息失败！");
                throw new BizException(RespCodeEnum.QUERY_AUTH_USER_IS_NULL.getRespCode(), RespCodeEnum.QUERY_AUTH_USER_IS_NULL.getRespDesc());
            }
            OrganizationMapper organizationMapper = new OrganizationMapperImpl();
            List<Organization> organizationList = organizationMapper.selectOrgInfoByorgCode(instId, authUser.getOrgCode());

            for (Organization organization : organizationList) {
                /*签约机构赋值*/
                mchntBaseInfo.setSignInstId(organization.getOrgCode());
                List<MchntBaseInfo> mchntBaseInfos = mchntBaseInfoMapper.selectBompMchntBaseInfoByInspectTasks(instId, mchntBaseInfo);
                if (null == mchntBaseInfos || mchntBaseInfos.isEmpty()) {
                    continue;
                }
                /*如果查询结果数据数量少于抽检数量全部*/
                SpotMchntBaseInfo(mchntBaseInfoSpotList, mchntBaseInfos, spotCheckMchntNum, instId);
            }
        } else {
            List<MchntBaseInfo> mchntBaseInfos = mchntBaseInfoMapper.selectBompMchntBaseInfoByInspectTasks(instId, mchntBaseInfo);
            if (null != mchntBaseInfos && !mchntBaseInfos.isEmpty()) {
                /*如果查询结果数据数量少于抽检数量全部*/
                SpotMchntBaseInfo(mchntBaseInfoSpotList, mchntBaseInfos, spotCheckMchntNum, instId);
            }
        }
        /*随机抽检商户没法使用公共的分页查询方法，自写分页代码*/
        int pageIndex = pageInfo.getPageIndex();
        int pageSize = pageInfo.getPageSize();
        int fromIndex = (-1 >= pageIndex || -1 >= pageSize) ? 0 : pageIndex * pageSize;
        int toIndex = (-1 >= pageIndex || -1 >= pageSize || fromIndex + pageSize > mchntBaseInfoSpotList.size()) ? mchntBaseInfoSpotList.size() : fromIndex + pageSize;
        pageInfo.setTotalPage((-1 >= pageSize || -1 >= pageIndex || pageSize > mchntBaseInfoSpotList.size()) ? 1 : mchntBaseInfoSpotList.size() / pageSize);
        pageInfo.setTotalRows(mchntBaseInfoSpotList.size());
        PageResult pageResult = new PageResult();
        pageResult.setPageInfo(pageInfo);
        pageResult.setResult(mchntBaseInfoSpotList.subList(fromIndex, toIndex));
        return pageResult;
    }

    /**
     * 抽检商户信息
     * mchntBaseInfoSpotList 抽检商户信息列表
     * mchntBaseInfos 查询商户信息列表
     */
    private void SpotMchntBaseInfo(List<Object> mchntBaseInfoSpotList, List<MchntBaseInfo> mchntBaseInfos, String spotCheckMchntNum, String instId) throws Exception {
        if (StringUtil.isEmptyOrNull(spotCheckMchntNum)) {
            BompSysDataDictMapper bompSysDataDictMapper = new BompSysDataDictMapperImpl();
            SysDataDict sysDataDict = bompSysDataDictMapper.selecBompParamInfoByPrimaryKey(instId, "spotCheckMchntNum");
            if (null == sysDataDict) {
                rglog.error("获取系统参数失败！");
                throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
            }
            spotCheckMchntNum = sysDataDict.getPrimaryvalue();
        }
        if (mchntBaseInfos.size() <= Integer.parseInt(spotCheckMchntNum)) {
            mchntBaseInfoSpotList.addAll(mchntBaseInfos);
        } else {
            /*随机获取相关数据*/
            int[] spotNum = new int[Integer.parseInt(spotCheckMchntNum)];
            int count = 0;
            while (count < spotNum.length) {
                int spotMchntIndex = random.nextInt(mchntBaseInfos.size() - 1);
                spotNum[count] = -1;
                boolean countFlag = Arrays.stream(spotNum).anyMatch(x -> x == spotMchntIndex);
                if (countFlag) {
                    continue;
                }
                spotNum[count] = spotMchntIndex;
                mchntBaseInfoSpotList.add(mchntBaseInfos.get(spotMchntIndex));
                count++;
            }
        }
    }

    /**
     * 交易量低商户巡检查询
     */
    private PageResult queryLowVolumeMchntInfoList(String lowVolumeMonth, String lowVolumeLessThan, String instId, MchntBaseInfo mchntBaseInfo, PageInfo pageInfo) throws Exception {
        //   List<Object>  mchntBaseInfoTransLowList = new ArrayList<>();
        /*交易月份和交易笔数值*/
        if (StringUtil.isEmptyOrNull(lowVolumeMonth)) {
            lowVolumeMonth = DateUtil.getCurrentDateTime("yyyyMM");
        } else {
            lowVolumeMonth = DateUtil.formatDate(new Date(), "yyyy") + lowVolumeMonth;
        }
        if (StringUtil.isEmptyOrNull(lowVolumeLessThan)) {
            BompSysDataDictMapper bompSysDataDictMapper = new BompSysDataDictMapperImpl();
            SysDataDict sysDataDict = bompSysDataDictMapper.selecBompParamInfoByPrimaryKey(instId, "lowVolumeLessThan");
            if (null == sysDataDict) {
                rglog.error("获取系统参数失败！");
                throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
            }
            lowVolumeLessThan = sysDataDict.getPrimaryvalue();
        }
        /*根据月份和交易数查询商户交易统计表中商户信息*/
        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        /*统计日期查询条件*/
        mchntBaseInfo.setRemark1(lowVolumeMonth);
        /*交易笔数查询条件*/
        mchntBaseInfo.setRemark2(lowVolumeLessThan);
        /*交易类型*/
        mchntBaseInfo.setRemark3(TxnTypeEnum.PAY.getTxnType());
        return mchntBaseInfoMapper.selectBompTxnStaticByInspectTask(mchntBaseInfo, pageInfo);
    }

    /**
     * 风险商户巡检
     */
    private PageResult queryRiskMchntInfoList(String instId, MchntBaseInfo mchntBaseInfo, PageInfo pageInfo) throws Exception {
        List<Object> mchntBaseInfoRiskList = new ArrayList<>();
        /*查询风险商户信息列表*/
        BompRiskMchntMapper bompRiskMchntMapper = new BompRiskMchntMapperImpl();
        List<RiskMchnt> riskMchntList = bompRiskMchntMapper.selectBompRiskMchntByinstId(instId);
        if (null == riskMchntList || riskMchntList.isEmpty()) {
            return null;
        }
        /*查询正常商户信息列表*/
        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        List<MchntBaseInfo> mchntBaseInfosQuery = mchntBaseInfoMapper.selectBompMchntBaseInfoByInspectTasks(instId, mchntBaseInfo);
        /*风险商户号*/
        Map<String, String> riskMchntMap = new HashMap<>();
        for (RiskMchnt riskMchnt : riskMchntList) {
            riskMchntMap.put(riskMchnt.getMchntNo(), riskMchnt.getMchntName());
        }

        for (MchntBaseInfo mchntBaseInf : mchntBaseInfosQuery) {
            if (StringUtil.isEmptyOrNull(riskMchntMap.get(mchntBaseInf.getMchntNo()))) {
                continue;
            }
            mchntBaseInfoRiskList.add(mchntBaseInf);
        }
        /*自写分页方法*/
        int pageIndex = pageInfo.getPageIndex();
        int pageSize = pageInfo.getPageSize();
        if (-1 == pageIndex && -1 == pageSize) {
            /*全部巡检，不分页*/
            PageResult pageResult = new PageResult();
            pageResult.setPageInfo(pageInfo);
            pageResult.setResult(mchntBaseInfoRiskList);
            return pageResult;
        }
        int fromIndex = pageIndex * pageSize;
        int toIndex = (fromIndex + pageSize > mchntBaseInfoRiskList.size()) ? mchntBaseInfoRiskList.size() : fromIndex + pageSize;
        pageInfo.setTotalPage((pageSize > mchntBaseInfoRiskList.size()) ? 1 : mchntBaseInfoRiskList.size() / pageSize);
        pageInfo.setTotalRows(mchntBaseInfoRiskList.size());
        PageResult pageResult = new PageResult();
        pageResult.setPageInfo(pageInfo);
        pageResult.setResult(mchntBaseInfoRiskList.subList(fromIndex, toIndex));
        return pageResult;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        InspectTaskMchntListQueryResponse inspectTaskMchntListQueryResponse = (InspectTaskMchntListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(inspectTaskMchntListQueryResponse);
    }
}